<!DOCTYPE html>
<html lang="en" style="font-size: 30px">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>touch实现拖拽</title>
    <style type="text/css">
        *{padding: 0px;margin: 0px;}
        #box {
            width: 100px;
            height: 100px;
            background: black;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>
<div id="box"></div>
<script>
    let box=document.querySelector("#box");
    let l = 0,
        t = 0;
    box.addEventListener("touchstart",(e) => {
        // console.log('touchstart')
        // e.touches[0].clientX  触点在视口中的 x 坐标
        // e.touches[0].clientY  触点在视口中的 y 坐标
        console.log('box offsetLeft', box.offsetLeft)
        console.log('box offsetTop', box.offsetTop)
        console.log('e.touches[0].clientX', e.touches[0].clientX)
        console.log('e.touches[0].clientY', e.touches[0].clientY)
        l = e.touches[0].clientX - box.offsetLeft;
        t = e.touches[0].clientY - box.offsetTop;
        // console.log('l = ',l,'t = ',t)
    },false);
    document.addEventListener("touchmove",(e) => {

        box.style.left = e.touches[0].clientX - l +"px";
        box.style.top = e.touches[0].clientY - t + "px";
        console.log('box.style.left', box.style.left)
        console.log('box.style.top', box.style.top)
    },false);

</script>
</body>
</html>
